*****************************************************************************
* Cleaning and Analyzing - PRITM countries. Alternative proxy				*
*   . Polarization proxy as Average distance in all the party system        *
*						Manifesto Project Database                          *
*  																            *
* Author: 			Valentina Gonzalez-Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			March 21 2021 									        *
* Version:			Stata 17						                        *
*																			*
*****************************************************************************

/*
This do-file:
	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: ** Manifesto Project database**
	- Data\CMP\MPDataset_MPDS2020a_stata14.dta // Data download from https://manifesto-project.wzb.eu/datasets 2021


Final output:
	Cleaned data: 
		* "Data\CMP_average.dta" this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated as the average distance of the party system.
	Tables:
		* table A21: Partisan Polarization over Redistribution and Fixed Attributes


*/

cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication" // Only change your directory

* Processing of the data (alternatively skip and go to line 158)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}
*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE Redistribution and Fixed Attributes
*############################################################
{
* Obtaining relevant policy variables

gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)

}

** Different code compared to 3_2 do file: 
***** Now average distance instead of distance between certain party families
{
egen av_welfare_policy= mean(welfare_policy), by(edate countryname)
gen dist_av_welfare_policy = abs(welfare_policy-av_welfare_policy)

egen av_fixed= mean(fixed), by(edate countryname)
gen dist_av_fixed = abs(fixed-av_fixed)

}
* Collapsing the data 
{
collapse (sum) av_welfare_policy dist_av_welfare_policy   av_fixed dist_av_fixed  (first) year  PRITM  totseats number2 oecdmember date  , by(edate countryname)
}
// Final Prep of the data
{
egen country_number = group(countryname)
sort country_number edate
bysort country_number: gen election_order=_n
xtset country_number election_order
sort countryname  election_order
sort countryname  year

gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994
xtset country_number election_order
}
}
*############################################
* Saving the data
*############################################
{

lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var PRITM "PRITM"

lab var dist_av_welfare_policy "Distance Redistribution (DR) - Net Welfare"
lab var dist_av_fixed "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"

lab var shock "High LMP period" // post 1994

keep  PRITM year  countryname  oecdmember totseats shock dist_av_welfare_policy dist_av_fixed   number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_average.dta", replace

}
}
* Alternatively load prepared data
{
use "Data\CMP_average.dta", clear	
}
*##########################################
* Analysis
*##########################################
// table A21: Partisan Polarization over Redistribution and Fixed Attributes
{

preserve
 
eststo clear
eststo: qui reg dist_av_welfare_policy L.dist_av_welfare_policy shock totseats  oecdmember  number2 i.year, cluster(countryname)
eststo: qui reg dist_av_fixed L.dist_av_fixed shock totseats  oecdmember  number2 i.year, cluster(countryname)

esttab , replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinpoldistav}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock  ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 

esttab using "Table\Tab_Within_d_av.tex", replace label se title(Partisan Polarization over Redistribution and Fixed Attributes  \label {TabWithinpoldistav}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock  ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tots*" "LDV = L.*" "FE Year = *year") 



restore

}
